﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using CasaBella.Entities;

namespace CasaBella.Repository
{
    public class SexoRepository:MasterRepository,ISexoRepository
    {
        public List<SEXO> GetAllFromSexo()
        {
            var query = from s in context.SEXOes
                        select s;
            return query.ToList();
        }

        public List<SEXO> GetAllFromSexoByCriterio(string criterio)
        {
            var query = from s in context.SEXOes
                        select s;
            if (criterio != null && criterio != string.Empty && criterio != "")
            {
                query = from s in context.SEXOes
                        where s.Descripcion.ToUpper().Contains(criterio)
                        select s;
            }
            return query.ToList();
        }

        public SEXO GetAllFRomSexoById(int id)
        {
            var query = from s in context.SEXOes
                        where s.Id.Equals(id)
                        select s;
            return query.SingleOrDefault();
        }

        public bool ExisteDuplicidad(string criterio, int id)
        {
            var query = from s in context.SEXOes
                        where s.Descripcion.CompareTo(criterio) == 0 &&
                        s.Id != id
                        select s;
            return query.Any();
        }

        public void AddSexo(SEXO sexo)
        {
            context.SEXOes.Add(sexo);
            context.SaveChanges();
        }

        public void UpdateSexo(SEXO sexo)
        {
            context.Entry(sexo).State = System.Data.EntityState.Modified;
            context.SaveChanges();
        }

        public void DeleteSexo(int id)
        {
            var existe = context.SEXOes.Find(id);

            if (existe != null)
            {
                context.SEXOes.Remove(existe);
                context.SaveChanges();
            }
        }
    }
}
